/*
 * Copyright 2015-2016 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
 *
 * You may not use this file except in compliance with the terms and conditions set forth
 * in the accompanying LICENSE.TXT file.
 *
 * THESE MATERIALS ARE PROVIDED ON AN "AS IS" BASIS.  AMAZON SPECIFICALLY DISCLAIMS, WITH
 * RESPECT TO THESE MATERIALS, ALL WARRANTIES, EXPRESS, IMPLIED, OR STATUTORY, INCLUDING
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
 */

package com.amazon.alexa.voicechrome.internal.views;

import android.animation.AnimatorSet;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.RelativeLayout;

import com.amazon.alexa.voicechrome.R;
import com.amazon.alexa.voicechrome.VoiceChromeVisuals;

import java.util.ArrayList;

public abstract class TransitionView extends RelativeLayout {

    AnimatorSet startAnimator;
    AnimatorSet stopAnimator;
    private static boolean loggingEnabled = VoiceChromeVisuals.loggingEnabled;
    private boolean isActive;
    private boolean useAnimation;
    protected int animationDuration;

    public TransitionView(Context context) {
        super(context);
        init();
    }

    public TransitionView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public TransitionView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        animationDuration = getResources().getInteger(R.integer.voicechrome_fade_duration);
        useAnimation = true;
        ArrayList<AnimatorSet> animators = defineAnimators();
        startAnimator = animators.get(0);
        stopAnimator = animators.get(1);
        setToStart();
    }

    public void pause() {
        if (loggingEnabled) Log.d(TransitionView.class.getSimpleName(), "pause()");
        cancelAnimations();
        setToStart();
        isActive = false;
    }

    public void resume() {
        if (loggingEnabled) Log.d(TransitionView.class.getSimpleName(), "resume()");
        cancelAnimations();
        setToEnd();
        isActive = true;
    }

    public void start() {
        if (loggingEnabled) Log.d(TransitionView.class.getSimpleName(), "start()");
        cancelAnimations();
        startAnimator.start();
        isActive = true;
    }

    public void stop() {
        if (loggingEnabled) Log.d(TransitionView.class.getSimpleName(), "stop()");
        cancelAnimations();
        if (useAnimation) {
            stopAnimator.start();
        } else {
            setToStart();
        }

        isActive = false;
    }

    private void cancelAnimations() {
        if (startAnimator.isRunning()) {
            startAnimator.cancel();
        }

        if (stopAnimator.isRunning()) {
            stopAnimator.cancel();
        }
    }

    public boolean isActive() {
        return this.isActive;
    }

    protected boolean isLoggingEnabled(){
        return loggingEnabled;
    }

    protected abstract ArrayList<AnimatorSet> defineAnimators();
    protected abstract void setToEnd();
    protected abstract void setToStart();

    public void setUseAnimation(boolean useAnimation) {
        this.useAnimation = useAnimation;
    }

}